package au.edu.unsw.editingtool.web;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import au.edu.unsw.editingtool.db.DBClaim;
import au.edu.unsw.editingtool.global.GlobalConstant;

/**
 * Servlet implementation class Main
 */
public class Main extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String ServletContext = null;
	private String ApplicationContext = null;	
	private String ApplicationPath = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Main() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doService(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doService(request, response);
	}
	protected void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		ServletContext = getServletContext().getRealPath("") + GlobalConstant.fileSeparator;
		ApplicationContext = request.getLocalName()+":"+request.getServerPort()+"/"+
						GlobalConstant.APPLICATIONNAME+"/";
		ApplicationPath = request.getContextPath();
		System.out.println("==== [MAIN Servlet] ====");
		
		String querystring = request.getQueryString();
		HttpSession session = request.getSession(false);
		session.setAttribute(GlobalConstant.requestLastCommand, GlobalConstant.cmdMain);
		System.out.println("[MAIN] Querystring : "+querystring);
		if (querystring == null)
			querystring ="";
		if(querystring.compareTo("null")==0)
			querystring ="";

		session.removeAttribute(GlobalConstant.requestFormData);					
		String username = (String) session.getAttribute(GlobalConstant.username);
		String sessionid = (String) session.getId();
		
		ArrayList<String> cc = new ArrayList<String>();
		ArrayList<String> cr = new ArrayList<String>();
		cc = this.grabClaimClaimed(username);
		cr = this.grabClaimRequest(username);
		
		if(cc.size()!=0)
			request.setAttribute(GlobalConstant.paramClaimClaimed,cc);
		if(cr.size()!=0)
			request.setAttribute(GlobalConstant.paramClaimClaimed,cr);
		
		System.out.println("[MAIN] Forward to "+GlobalConstant.JSPMain);				
		RequestDispatcher rd = request.getRequestDispatcher(GlobalConstant.JSPMain+"?"+querystring);
		rd.forward(request, response);
	}
	
	private ArrayList<String> grabClaimClaimed(String username)
	{
		DBClaim dbclaim = new DBClaim();
		dbclaim.setConfFile(ServletContext + GlobalConstant.configFilePath);
		
		ArrayList<String> al = new ArrayList<String>();
		try
		{
			al = dbclaim.getResources(username, GlobalConstant.claimClaimed);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}		
		return al;
	}
	
	private ArrayList<String> grabClaimRequest(String username)
	{
		DBClaim dbclaim = new DBClaim();
		dbclaim.setConfFile(ServletContext + GlobalConstant.configFilePath);
		
		ArrayList<String> al = new ArrayList<String>();
		try
		{
			al = dbclaim.getResources(username, GlobalConstant.claimRequest);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}		
		return al;
	}
}
